import { Inject, Post, Body } from '@midwayjs/core';
import { CoolController, BaseController } from '@cool-midway/core';
import { CouponUserEntity } from '../../entity/user';
import { CouponUserService } from '../../service/user';
import { CouponInfoEntity } from '../../entity/info';
import { StudentStudentEntity } from '../../../student/entity/student';

/**
 * 用户优惠券
 */
@CoolController({
  api: ['add', 'delete', 'update', 'info', 'list', 'page'],
  entity: CouponUserEntity,
  service: CouponUserService,
  insertParam: (ctx) => {
    return {
      'receiveTime': new Date(),
    };
  },
  pageQueryOp: {
    select: [
      'a.*',
      'b.name as couponName',
      'b.type as couponType',
      'b.discountAmount',
      'b.discountRatio',
      'b.thresholdAmount',
      'c.id as studentId',
      'c.name as studentName',
      'c.phone as studentPhone',
    ],
    fieldEq: ['a.userId', 'a.status', 'a.couponId'],
    join: [
      {
        entity: CouponInfoEntity,
        alias: 'b',
        condition: 'a.couponId = b.id',
        type: 'leftJoin',
      },
      {
        entity: StudentStudentEntity,
        alias: 'c',
        condition: 'a.userId = c.id',
        type: 'leftJoin',
      },
    ],
    where: async ctx => {
      const { } = ctx.request.body;
      const where = [];
      return where;
    },
  },
})
export class AdminCouponUserController extends BaseController {
  @Inject()
  couponUserService: CouponUserService;

  /**
   * 获取用户可用优惠券列表
   */
  @Post('/userCanUseCouponList')
  async userCanUseCouponList(@Body() param: { userId: number }) {
    return this.ok(await this.couponUserService.userCanUseCouponList(param.userId));
  }
}
